package com.humanmonth.util.effect{
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import flash.filters.GlowFilter;
	
	import mx.core.Application;
	/**
	 * 让一般的DisplayObject具添加拖动的能力 
	 * @author 林子龙
	 * 
	 */
	public class MoveHelper{
		private var obj:DisplayObject;
		private var offsetx:Number;
		private var offsety:Number;

		public function MoveHelper(obj:DisplayObject) {
			this.obj=obj;
			obj.addEventListener(MouseEvent.MOUSE_DOWN,initMove);
			Application.application.addEventListener(MouseEvent.MOUSE_UP,endMove);
		}
		
		/**
		 * 鼠标按下时，初始化偏移量及增加移动监听器 
		 */
		private function initMove(event:MouseEvent):void {
			offsetx=event.stageX-obj.x;
			offsety=event.stageY-obj.y;
			Application.application.addEventListener(MouseEvent.MOUSE_MOVE,dealMove);
		}
		
		
		/**
		 * 移动时，改变坐标值 
		 */
		private function dealMove(event:MouseEvent):void {
			var tempX:Number=event.stageX-offsetx;
			var tempY:Number=event.stageY-offsety;
			if(tempX<0){
				obj.x=0;
			}else{
				obj.x=tempX;
			}	
			if(tempY<0){
				obj.y=0;
			}else{
				obj.y=tempY;
			}
		}

		/**
		 * 鼠标放开，不移动时的处理 
		 */
		private function endMove(event:MouseEvent):void {
			Application.application.removeEventListener(MouseEvent.MOUSE_MOVE,dealMove);
		}
	}
}